<!--
* @description 
* @fileName login.vue
* @author huangyu
* @date 2023/03/04 15:43:59
!-->
<template>
    <div>
        <div style="text-align: center;line-height:60px;border-bottom:1px solid #f2f2f2;"><strong>登录</strong></div>
        <van-form @submit="onSubmit">
            <van-field
                v-model="ruleForm.gameCode"
                name="摆摊号"
                label="摆摊号"
                placeholder="摆摊号"
                :rules="[{ required: true, message: '请填写摆摊号' }]"
            />
            <!-- <van-field
                v-model="ruleForm.password"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            /> -->
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">立即登录</van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import { get } from '@/common/app.js'
import * as types from "@/store/types";
// import md5 from 'js-md5';
import { Notify } from 'vant';
export default {
    components: {},
    data() {
        return {
            token: "",
            ruleForm: {
                gameCode: "",
                password: "",
            },
        }
    },
    mounted() {

    },
    methods: {
        onSubmit() {
            // console.log('submit', values);
            this.loginData()
        },
        loginData(){
            // this.ruleForm.password = md5(this.ruleForm.password)
            let params = {
                type: 'getLogin',
                api: 'api-login',
                data: this.ruleForm,
            }
            // console.log(params)
            get('game', params).then(res => {
                if (res) {
                    // console.log(res)
                    if(res.type=='error'){
                        Notify({ type: 'danger', message: res.msg })
                        return
                    }

                    this.token = res._id
                    // 存储token并跳转
                    if(this.token){
                        let logindata = {
                            token: this.token,
                            user: {
                                gameCode: res.gameCode,
                                gameName: res.gameName,
                                qq: res.qq,
                                gameId: res.gameId,
                            },
                            power: res.powerInfo,
                        }
                        Notify({ type: 'success', message: '登录成功！' })
                        this.$store.commit(types.LOGIN, logindata);
                        this.$emit('onLogin',true)
                        this.$router.go(0)
                    }
                }
            })
        },
    },
}
</script>

<style scoped></style>